import path from "path"
import * as glob from "glob"
import { desktopPath } from "@/utils"
import { email } from "@/services"

import { type Tasks } from "@/interface"

export default async (tasks: Tasks[]) => {
  const folders = [...new Set(tasks.map(i => i.folder))]

  const options = { windowsPathsNoEscape: true }

  for (const folder of folders) {
    const courses = glob.sync(
      path.join(desktopPath, `t_${folder}`, "*/"),
      options,
    )

    if (courses.length <= 15) {
      await email({
        folder: folder,
        subject: `${folder} 资料剩余 ${courses.length}个，请及时补充`,
        text: "",
      })
    }
  }
}
